﻿--[[
   @brief    unlock_branch view
   @author   jeffrywang
   @version  1.0
   @date     2017/02/14
--]]

unlock_branch_view = {
    unlock_branch_dlg_closed_ = true;
    game_id_ = nil;
    branch_info_ = nil;
    
    show_dlg = function(branch_info)
        branch_info_ = branch_info;
        unlock_branch_view.unlock_branch_dlg_closed_ = false;
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]show_dlg");
        local dlg_cid = CID_UNLOCK_BRANCH_DLG;
        local dlg = ui_utility.get_ctrl_ptr_by_cid(CID_UNLOCK_BRANCH_DLG);
        if dlg == nil or dlg == 0 then
            layout_mgr.restore_layout("unlock_branch.tpf");
        end;
        dlg = ui_utility.get_ctrl_ptr_by_cid(CID_UNLOCK_BRANCH_DLG);
        if dlg ~= nil or dlg ~= 0 then
            --dlg:MoveToCenter();
            --dlg:Show(TRUE);
            local uilib = ui_utility.uilib();
            local control_id = uilib:GetControlIDbyCustomID(CID_UNLOCK_BRANCH_DLG);
            ui_utility.do_fake_modal_by_id(control_id);
        end;
        unlock_branch_view.show_error(FALSE);
        unlock_branch_view.clear_code();
    end;
    
    close_dlg = function()
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]close_dlg");
        unlock_branch_view.show_error(FALSE);
        if not unlock_branch_view.unlock_branch_dlg_closed_ then
            unlock_branch_view.unlock_branch_dlg_closed_ = true;

            local dlg = ui_utility.get_ctrl_ptr_by_cid(CID_UNLOCK_BRANCH_DLG);
            if dlg ~= nil or dlg ~= 0 then
                dlg:Show(FALSE);
            end;
            local uilib = ui_utility.uilib();
            local control_id = uilib:GetControlIDbyCustomID(CID_UNLOCK_BRANCH_DLG);
            layout_mgr.delay_remove_ctrl(control_id);
        end;
    end;
    
    on_close_dlg = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]on_close_dlg");
        unlock_branch_view.close_dlg();
    end;

    on_unlock = function(uilib, dwID_Click)
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]on_unlock_btn_click");
        unlock_branch_control.unlock_branch_check();
    end;
    
    get_code = function()
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_UNLOCK_BRANCH_EDIT_CODE);
        if component ~= nil and component ~= 0 then
            local code = component:GetText();
            local code_u8 , str_holder = CUIStrtoU8(code);
            return code_u8;
        end;
        return nil;
    end;
    
    clear_code = function()
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]clear_code");
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_UNLOCK_BRANCH_EDIT_CODE);
        if component ~= nil and component ~= 0 then
            local const_ui_str = U8toCUIStr("");
            component:SetText(const_ui_str);
            component:SetFocus();
        end;
    end;
    
    on_clear_code = function()
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]on_clear_code");
        unlock_branch_view.clear_code();
    end;
    
    show_error = function(bshow)
        ierd_tgp.tpf_ui.Log("[unlock_branch_view]show_error");
        local txt_err = ui_utility.get_ctrl_ptr_by_cid(CID_UNLOCK_BRANCH_ERR_TXT);
        if txt_err ~= nil and txt_err ~= 0 then
            txt_err:Show(bshow);
        end;
    end;
    
    -- 获得焦点时,将错误隐藏
    on_set_focus = function()
        unlock_branch_view.show_error(FALSE);
    end;
};
